<template>
  <div id="Detail">
    <detail-nav-bar class="detail-nav" ref="nav" @tabClick="tabClick(index)" />
    <scroll 
    class="content"
    ref="scroll"
    :probe-type="3"
    :observeDOM="true"
    :observeImage="true"
    @scroll="contentScroll"
    >
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shopInfo"/>
      <detail-goods-info 
      :detailInfo="detailInfo"
      @imageLoad="imageLoad"
      />
      <detail-param-info :param-info="paramInfo" ref="params" />
      <detail-comment-info :comment-info="commentInfo" ref="comment"/>
      <goods-list :goods="recommends" ref="recommends"/>
    </scroll>
    <!-- 底部导航 -->
    <detail-bottom-bar class="bottom-bar" @addToCart="addToCart" />
    <!-- 回到顶部按钮 -->    
    <back-top @click="backClick" v-show="isBackShow" class="back-top"/>
    <!-- 弹窗 -->
    <toast :message="message" :isShow="isShow" />
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar.vue'
import DetailSwiper from './childComps/DetailSwiper.vue'
import DetailBaseInfo from './childComps/DetailBaseInfo.vue'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParamInfo from './childComps/DetailParamInfo.vue'
import DetailCommentInfo from './childComps/DetailCommentInfo.vue'
import DetailBottomBar from './childComps/DetailBottomBar.vue'

import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'
import Toast from 'components/common/toast/Toast'

import {getDetail,getRecommend,Goods,GoodsParam} from 'network/detail.js'
import {debounce} from 'common/utils.js'
import {backTopMixin} from 'common/mixin.js'


export default {
  name: 'Detail',
  components: {
    Scroll,
    Toast,
    GoodsList,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar
  },
  mixins: [backTopMixin],
  data() {
    return {
      iid: null,
      goods: {},
      shopInfo: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [], //记录滚动y轴坐标
      getThemeTopY: null, //防抖函数
      currentIndex: 0,
      product: {},
      isShow: false,
      message: '',
      detail_nav: 0     
    }
  },
  created() {
    //保存iid
    this.iid = this.$route.qurey.iid
    //传入detail数据
    getDetail(this.iid).then(res => {
      const data = res.result
      this.topImages = data.itemInfo.topImages
      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      )
      // 3.获取店铺信息
      this.shopInfo = data.shopInfo;
      // 4.获取商品详细信息
      this.detailInfo = data.detailInfo;
      // 5.获取参数的信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      )
      // 6.获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    }) 
    // 3.获取推荐数据
    getRecommend().then((res) => {
      this.recommends = res.data.list;
    })
    //标题与主体联动
    this.$nextTick(() => {
      this.getThemeTopY = debounce(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.recommends.$el.offsetTop - 44)
        this.themeTopYs.push(Number.MAX_VALUE); // 存入最大值，用于边界条件判断
      },200)
    })

  },

  methods: {
    imageLoad() {
      this.$refs.scroll.refresh()
      this.getThemeTopY()
    },
    tabClick(index) {
      this.$refs.scroll.scrollIo(0 , -this.themeTopYs[index] ,200)
    },
    contentScroll(position) {
      //1.获取y值
      const positionY = -position.y
      //2.positionY和主题中的值对比
      for (let i = 0; i < this.themeTopYs.length - 1; i++) {
        if (
          this.currentIndex !== i &&
          positionY > this.themeTopYs[i] &&
          positionY <= this.themeTopYs[i+1]
          ) {
          this.currentIndex = i //用于避免重复记录i的次数
          this.$refs.nav.currentIndex = this.currentIndex
        }
        
      }
      this.isBackShow = (-position.y) > 1000
    },
    addToCart () {
      //定义一个product变量，类型时对象
      this.product.iid = this.iid
      this.product.img = this.topImages[0]
      this.product.title = this.goods.title
      this.product.desc = this.goods.desc
      this.product.newPrice = this.goods.realPrice
      // 添加到购物车 dispatch向后台提供数据 添加成功后使用异步操作显示toast内容
      this.$store.dispatch('addCart',this.product).then(res => {
        this.message = res
        this.isShow = true
        //3ms后清空toast内容
        setTimeout(() => {
          this.message = ''
          this.isShow = false
        }, 1500);
      })
    }
  },
}
</script>

<style scoped>
.detail {
  /* 遮蔽tabbar */
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.detail .goods-info {
  position: absolute;
}
.detail-nav {
  position: absolute;
  z-index: 9;
  background-color: #fff;
  font-weight: 700;
}
.content{
  height: calc(100% - 102px);
  overflow: hidden;
}
.bottom-bar {
  position: absolute;
}
</style>